//
//  AboutUsViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/7.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "AboutUsViewController.h"
#import "AboutUsTableViewCell.h"
#import "CommonViewController.h"
#import "NormalCommunalController.h"
@interface AboutUsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UIView * footerView;
@property (nonatomic, strong)NSMutableArray * cellData;
@end

@implementation AboutUsViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - BOTTOM_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 58/WIDTH_6_SCALE;
        [_tableView registerClass:[AboutUsTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AboutUsTableViewCell class])];
        _tableView.backgroundColor = getColor(mainColor);
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
    }return _tableView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[@{@"title":@"版权信息"},
                      @{@"title":@"软件许可协议"},
                      @{@"title":@"一米团服务协议"},
                      @{@"title":@"隐私权政策"}].mutableCopy;
    }return _cellData;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160/WIDTH_6_SCALE)];
        _headerView.backgroundColor = [UIColor clearColor];
        //logo
        UIImageView * iconImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20/WIDTH_6_SCALE, 100/WIDTH_6_SCALE, 100/WIDTH_6_SCALE)];
        iconImg.centerX = _headerView.centerX;
        iconImg.image = [UIImage imageNamed:@"martLogo"];
        [_headerView addSubview:iconImg];
        //app名字
        UILabel * appTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(10/WIDTH_6_SCALE, iconImg.bottom + 10/WIDTH_6_SCALE, SCREEN_WIDTH - 20/WIDTH_6_SCALE, 15/WIDTH_6_SCALE)];
        [self label:appTitleLab withFont:DEF_FontSize_16 withText:@"一米团" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentCenter withView:_headerView];
        //权利
        UILabel * appRightLab = [[UILabel alloc] initWithFrame:CGRectMake(appTitleLab.left, appTitleLab.bottom + 10/WIDTH_6_SCALE, appTitleLab.width, appTitleLab.height)];
        [self label:appRightLab withFont:DEF_FontSize_14 withText:@"Apple v1.0.2 2018121212" withTextColor:getColor(@"afafb0") withTextAlignment:NSTextAlignmentCenter withView:_headerView];
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, appRightLab.bottom + 10/WIDTH_6_SCALE);
    }return _headerView;
}
- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100/WIDTH_6_SCALE)];
        _footerView.backgroundColor = [UIColor clearColor];
        //检查更新
        UIButton * updataBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 20/WIDTH_6_SCALE, 100/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
        updataBtn.centerX = _headerView.centerX;
        [self button:updataBtn withBGColor:getColor(mainRedColor) withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:4 withMasksToBounds:YES withViwe:_footerView];
        [updataBtn setTitle:@"检查更新" forState:UIControlStateNormal];
        [updataBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [updataBtn addTarget:self action:@selector(updataBtnClick) forControlEvents:UIControlEventTouchUpInside];
        updataBtn.titleLabel.font = DEF_FontSize_14;
        //版权
        UILabel * rightLab = [[UILabel alloc] initWithFrame:CGRectMake(0, updataBtn.bottom + 15/WIDTH_6_SCALE, SCREEN_WIDTH, 40/WIDTH_6_SCALE)];
        [self label:rightLab withFont:DEF_FontSize_12 withText:@"沈阳圣讯科技有限公司  版权所有\nCopyright@2016-2019" withTextColor:getColor(@"afafb0") withTextAlignment:NSTextAlignmentCenter withView:_footerView];
        rightLab.numberOfLines = 0;
        _footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, rightLab.bottom + 15/WIDTH_6_SCALE);
    }return _footerView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AboutUsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AboutUsTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellData.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NormalCommunalController * vc = [[NormalCommunalController alloc] init];
    if (indexPath.row == 0) {
        vc.navigationItem.title = @"版权信息";
        vc.pageType = @"2";
    }
    if (indexPath.row == 1) {
        vc.navigationItem.title = @"软件许可协议";
        vc.pageType = @"4";
    }
    if (indexPath.row == 2) {
        vc.navigationItem.title = @"一米团服务协议";
        vc.pageType = @"5";
    }
    if (indexPath.row == 3) {
        vc.navigationItem.title = @"隐私政策";
        vc.pageType = @"6";
    }
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - action
- (void)updataBtnClick{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    CFShow((__bridge CFTypeRef)(infoDictionary));
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    ShowMessage(app_Version);
}
@end
